<?php
/**
 * (logic) FBA费用
 */
namespace App\Logic\MonthlyReport\FbaFee;

use App\Models\MonthlyReport\Payment\PaymentFBAFee;
use App\Models\MonthlyReport\Tmp\TmpOrder;
class MatchFBAFeeOrder
{
    public static function exe(){
        $date = date('Y-m-01',strtotime('midnight first day of -1 month'));
        ini_set('memory_limit', '200M');
        set_time_limit(0);
        $order = [];
        PaymentFBAFee::query()
            ->where('report_date',$date)
            ->orderBy('id')
            ->chunk(2000, function ($lines) use (&$order){
                foreach ($lines as $line){
                    if(!is_null($line->purchase_date))continue;
                    if(!array_key_exists($line->order_id,$order)){
                        $_purchaseDate = TmpOrder::query()
                            ->where('order_id',$line->order_id)
                            ->value('purchase_date');
                        $order[$line->order_id] = $_purchaseDate ?: null;
                    }
                    $purchaseDate = $order[$line->order_id];
                    if(is_null($purchaseDate))continue;
                    $line->purchase_date = $purchaseDate;
                    $line->save();
                }
            });
    }
}
